$(function () {
    //加载查询条件中的报价公司下拉框
    $("#kcbj_bjgs").combotree({
        url:$.webPath()+'/organ/gsjg/gsjg_tree?type=1',
        panelHeight:'200',lines:'true',
        loadFilter: function (rows) {
            return convert(rows);
        },
        onLoadSuccess:function(){
            //表格初始加载
            $('#grid_kcbj').datagrid({
                url:$.webPath() + "/xsgl/kcbj/kcbjList",
                toolbar:"#toolbar_kcbj",
                fitColumns:true,
                singleSelect:true,
                pagination:true,
                fit:true,
                rownumbers:true,
                pageNumber:1,
                striped:true,
                columns:[[
                    {field:'bjgs.jc', title:'报价公司',align: 'center', width:30},
                    {field:'bjrq', title:'报价日期',align: 'center', width:20},
                    {field:'gg.name', title:'物资名称',align: 'center', width:20},
                    {field:'gg.ggxh', title:'规格型号',align: 'center', width:20},
                    {field:'gg.dw', title:'单位',align: 'center', width:20},
                    {field:'cd.z', title:'产地',align: 'center', width:20},
                    {field:'kcsl', title:'库存数量',align: 'right', width:20,
                        formatter:function(value){
                            var val = formatSlForHtml(value);
                            return val;
                        }
                    },
                    {field:'kkdsl', title:'可开单数量',align: 'right', width:20,
                        formatter:function(value){
                            var val = formatSlForHtml(value);
                            return val;
                        }
                    },
                    {field:'bj', title:'报价',align: 'right', width:20,
                        formatter:function(value){
                            var val = formatJeForHtml(value);
                            return val;
                        }
                    },
                    {field:'hdbj', title:'核对报价',align: 'right', width:20,
                        formatter:function(value){
                            var val = formatJeForHtml(value);
                            return val;
                        }
                    },
                    {field:'scbj', title:'市场报价',align: 'right', width:20,
                        formatter:function(value){
                            var val = formatJeForHtml(value);
                            return val;
                        }
                    },
                    {field:'hdscbj', title:'核对市场报价',align: 'right', width:20,
                        formatter:function(value){
                            var val = formatJeForHtml(value);
                            return val;
                        }
                    },
                    {field:'wj', title:'网价',align: 'right', width:20,
                        formatter:function(value){
                            var val = formatJeForHtml(value);
                            return val;
                        }
                    },
                    {field:'hdwj', title:'核对网价',align: 'right', width:20,
                        formatter:function(value){
                            var val = formatJeForHtml(value);
                            return val;
                        }
                    }
                ]],
                queryParams: {
                    bjrq:$("#toolbar_kcbj #kcbj_bjrq").datebox("getValue") || '',
                    bjgsId:$("#toolbar_kcbj #kcbj_bjgs").combotree("getValue") || ''
                }
            });

        }
    });
    //**********按钮触发事件**********
    //库存管理按钮
    $("#btn_kcgl").click(function () {
        openKcglDialog('报价物资管理', 'save_kc');
    });
    //库存报价
    $("#btn_kcbj").click(function () {
        var win = $.window({
            title:'库存报价',
            width:915,
            height:450,
            cache:false,
            url:$.webPath() + "/xsgl/kcbj/kcbj_content",
            modal:true,
            buttons:[
                {
                    text:'保存',
                    handler:function () {
                        $(win).mask();
                        $("#kcbj_bj").edatagrid("saveRow");
                        $('#form_kcbj_bj').form('submit', {
                            url:$.webPath() + "/xsgl/kcbj/save_kcbj",
                            onSubmit:function (param) {
                                var data = $("#kcbj_bj").edatagrid("getRows");
                                if(data.length <= 0){
                                    $(win).mask("hide");
                                    $.messager.alert('提示', '无任何数据', 'info');
                                    return false;
                                }
                                var bjrq = $("#bj_bjrq").datebox("getValue");
                                data = $.map(data,function(value,index){
                                    var d = {};
                                    d['bjgs.id'] = value['gsId'];
                                    d["gg.id"] = value['gg.id'];
                                    d["cd.id"] = value['cd.id'];
                                    d["bjrq"] = bjrq;
                                    d["bj"] = value.bj;
                                    d["scbj"] = value.scbj;
                                    d["wj"] = value.wj;
                                    return d;
                                });
                                var paramString = "{";
                                for(var i=0;i<data.length;i++){
                                    var objectString = $.toJSONStringPrefix(data[i], "kcbjList" + "[" + i + "].");
                                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                }
                                if (data && data.length > 0)
                                    paramString = paramString.substring(0, paramString.length - 1);
                                paramString += "}";
                                $.extend(param, $.parseJSON(paramString));
                                return $(this).form('validate');
                            },
                            success:function (data) {
                                $(win).mask('hide');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window('close');
                                    $('#grid_kcbj').datagrid('reload');
                                    $.messager.show({
                                        title:'提示',
                                        msg:json.message,
                                        timeout:3000,
                                        showType:'slide'
                                    });
                                } else
                                    $.messager.alert('提示', json.message, 'error');
                            }
                        });
                    }
                }
            ]
        });
    });
    //核对报价
    $("#btn_hdbj").click(function(){
        var win = $.window({
            title:'核对报价',
            width:915,
            height:450,
            cache:false,
            url:$.webPath() + "/xsgl/kcbj/hdbj_content",
            modal:true,
            buttons:[
                {
                    text:'保存',
                    handler:function () {
                        $(win).mask();
                        $("#hdbj_bj").edatagrid("saveRow");
                        $('#form_hdbj').form('submit', {
                            url:$.webPath() + "/xsgl/kcbj/save_hdkcbj",
                            onSubmit:function (param) {
                                var data = $("#hdbj_bj").edatagrid("getRows");
//                                if(data.length <= 0){
//                                    $(win).mask("hide");
//                                    $.messager.alert('提示', '无数据修改', 'info');
//                                    return false;
//                                }
                                var bjrq = $("#hdbj_bjrq").datebox("getValue");
                                data = $.map(data,function(value,index){
                                    var d = {};
                                    d["id"] = value.id || '';
                                    d["bjrq"] = bjrq;
                                    d["hdbj"] = value.hdbj;
                                    d["hdscbj"] = value.hdscbj;
                                    d["hdwj"] = value.hdwj;
                                    return d;
                                });
                                var paramString = "{";
                                for(var i=0;i<data.length;i++){
                                    var objectString = $.toJSONStringPrefix(data[i], "kcbjList" + "[" + i + "].");
                                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                }
                                if (data && data.length > 0)
                                    paramString = paramString.substring(0, paramString.length - 1);
                                paramString += "}";
                                $.extend(param, $.parseJSON(paramString));
                                return $(this).form('validate');
                            },
                            success:function (data) {
                                $(win).mask('hide');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window('close');
                                    $('#grid_kcbj').datagrid('reload');
                                    $.messager.show({
                                        title:'提示',
                                        msg:json.message,
                                        timeout:3000,
                                        showType:'slide'
                                    });
                                } else
                                    $.messager.alert('提示', json.message, 'error');
                            }
                        });
                    }
                }
            ]
        });
    })
    //查询按钮
    $("#btn_kcbj_search").click(function () {
        kcbj_search();
    });
});

//库存管理
function openKcglDialog(title, action) {
    var href = $.webPath() + "/xsgl/kcbj/kcgl_content";
    var actionUrl = $.webPath() + "/xsgl/kcbj/" + action;
    showKcglDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function showKcglDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:450,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $("#kcbj_kcmx").edatagrid("saveRow");
                    $('#form_kcbj').form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            var data = $("#kcbj_kcmx").edatagrid("getRows");
                            alert(data.length);
                            if(data.length <= 0){
                                $(win).mask("hide");
                                $.messager.alert('提示', '无数据修改', 'info');
                                return false;
                            }
                            var bjgsId = $("#kc_bjgs").combotree("getValue");
                            var i = 0;
                            for (var j=0;j<data.length;j++){
                                param["kcList["+i+"].id"] = data[j]["id"];
                                param["kcList["+i+"].gg.id"] = data[j]["gg.id"];
                                param["kcList["+i+"].cd.id"] = data[j]["cd.id"];
                                param["kcList["+i+"].bjgs.id"] = bjgsId;
                                i+=1;
                            }
                            return $(this).form('validate');
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_kcbj').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//查询库存报价
function kcbj_search() {
    $('#grid_kcbj').datagrid('reload', {
        bjrq:$("#toolbar_kcbj #kcbj_bjrq").datebox("getValue") || '',
        bjgsId:$("#toolbar_kcbj #kcbj_bjgs").combotree("getValue") || ''
    });
}




